package edu.tjut.tjutcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.tjut.tjutcommunity.entity.PageResult;
import edu.tjut.tjutcommunity.entity.Post;
import edu.tjut.tjutcommunity.entity.Star;
import edu.tjut.tjutcommunity.mapper.StarMapper;
import edu.tjut.tjutcommunity.service.PostService;
import edu.tjut.tjutcommunity.service.StarService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhh
 * @since 2022-05-01
 */
@Service
public class StarServiceImpl extends ServiceImpl<StarMapper, Star> implements StarService {

    final
    StarMapper starMapper;

    public StarServiceImpl(StarMapper starMapper, PostService postService) {
        this.starMapper = starMapper;
        this.postService = postService;
    }

    final
    PostService postService;

    @Override
    public boolean star(Integer uid, Integer pid) {
        QueryWrapper<Star> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("pid", pid);
        Star star = starMapper.selectOne(qw);
        if (star != null) {
            return false;
        }
        star = new Star();
        star.setPid(pid);
        star.setUid(uid);
        return starMapper.insert(star) == 1;
    }
    @Override
    public boolean unstar(Integer uid, Integer pid) {
        QueryWrapper<Star> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("pid", pid);
        Star star = starMapper.selectOne(qw);
        if (star != null) {
            starMapper.deleteById(star.getSid());
            return true;
        }
        return false;
    }


    @Override
    public PageResult<Post> myStar(Integer uid, Integer pageNo, String keyword) {
        int pageSize = 10;
        Page<Star> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Star> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        if (keyword != null && !"".equals(keyword)) {
            queryWrapper.like("title", keyword)
                    .or()
                    .like("description", keyword);
        }

        queryWrapper.orderByDesc("update_time");
        Page<Star> starPage = starMapper.selectPage(page, queryWrapper);
        Set<Integer> pids = starPage.getRecords().stream().map(Star::getPid).collect(Collectors.toSet());
        List<Post> posts = null;
        if (!pids.isEmpty()) {
            posts = postService.getPostByIds(pids);
            Collections.reverse(posts);
        } else {
            posts = Collections.emptyList();
        }

        return new PageResult<>(posts, starPage.getPages());
    }

    @Override
    public boolean isStar(Integer uid, String pid) {
        QueryWrapper<Star> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("pid", pid);
        return starMapper.selectCount(qw) == 1;
    }
}
